package zebbe.android.opengl.screens;

import javax.microedition.khronos.opengles.GL10;

import zebbe.android.opengl.Renderer;
import zebbe.android.opengl.Simulation;
import zebbe.android.opengl.support.GameActivity;
import zebbe.android.opengl.support.GameScreen;
import zebbe.android.opengl.support.SimulationListener;

public class GameLoop implements GameScreen, SimulationListener
{
        public Simulation simulation;
        Renderer renderer;     
        
        private float mPreviousX;
        private float mPreviousY;
//        SoundManager soundManager;

        public GameLoop( GL10 gl, GameActivity activity )
        {
                simulation = new Simulation();
                simulation.listener = this;
                renderer = new Renderer( gl, activity );
//                soundManager = new SoundManager( activity );
        }
       
        public GameLoop(GL10 gl, GameActivity activity, Simulation simulation)
        {
                this.simulation = simulation;
                this.simulation.listener = this;
                renderer = new Renderer( gl, activity );
//                soundManager = new SoundManager( activity );
        }

        @Override
        public void render(GL10 gl, GameActivity activity)
        {      
                renderer.render( gl, activity, simulation);
        }

        @Override
        public void update(GameActivity activity)
        {      
                processInput( activity );
                simulation.update( activity.getDeltaTime() );
        }
       
        private void processInput( GameActivity activity )
        {              
//                if( activity.getAccelerationOnYAxis() < 0 )
//                        simulation.rotateCube(0f, activity.getAccelerationOnYAxis());
  
//                else
//                        simulation.moveShipRight( activity.getDeltaTime(), Math.abs(activity.getAccelerationOnYAxis()) / 10 );
//       
//               
//                if( activity.isTouched() )
//                        simulation.shot();
        	if (activity.isTouched()) {
        		
        		float x = activity.getTouchX();
        		float y = activity.getTouchY();
        		
        		float dx = x - mPreviousX;
                float dy = y - mPreviousY;
                
                mPreviousX = x;
                mPreviousY = y;
                
//        		simulation.rotateCube(dx, dy);
//        		simulation.translateCube(dx, -dy);
                simulation.setCubeVelocity(x, y);
        	}
        }

        public boolean isDone( )
        {
//                return simulation.ship.lives == 0;
        	return false;
        }
       
        public void dispose( )
        {
                renderer.dispose();
//                soundManager.dispose();
        }

}


